//
//  OBDHUD.swift
//  OBD
//
//  Created by sqkj on 2021/9/17.
//

import UIKit
import MBProgressHUD

class OBDHUD: NSObject {
    public class func showText(text: String,mode:MBProgressHUDMode) {
        let view = viewWithShow()
        
        let hud = MBProgressHUD.showAdded(to: view, animated: true)
        hud.label.text = text
        hud.mode = mode
        hud.removeFromSuperViewOnHide = true
        hud.hide(animated: true, afterDelay: 2)
    }
    
   public class func viewWithShow() -> UIView {
        var window = UIApplication.shared.keyWindow
        if window?.windowLevel != UIWindow.Level.normal {
            let windowArray = UIApplication.shared.windows
            
            for tempWin in windowArray {
                if tempWin.windowLevel == UIWindow.Level.normal {
                    window = tempWin;
                    break
                }
            }
        }
        return window!
    }
    
    public class func showLoading(_ info: String = "",view:UIView,time:TimeInterval = 0){
        let hud = MBProgressHUD.showAdded(to: view, animated: true)
        hud.label.text = info
        hud.mode = MBProgressHUDMode.indeterminate
        hud.removeFromSuperViewOnHide = true
        if time > 0 {
            OBDGCDTimer.start(1, time) { isFinish,count in
                if isFinish{
                    self.dismiss(view: view)
                }
            }
        }
    }
    
    public class func showLoading(_ info: String = "",time:TimeInterval = 0) {
        let view = viewWithShow()
        self.showLoading(view: view,time: time)
    }
    
    public class func dismiss(view:UIView) {
        MBProgressHUD.hide(for: view, animated: true)
    }
    
    public class func dismiss() {
        let view = viewWithShow()
        self.dismiss(view:view)
    }
    
    public class func showMessage(_ status: String) {
        self.dismiss()
        showText(text: status,mode:MBProgressHUDMode.text)
    }
    
    public class func showSuccess(_ status: String) {
        self.dismiss()
        showText(text: status,mode:MBProgressHUDMode.text)
    }
    
    public class func showError(_ status: String) {
        self.dismiss()
        showText(text: status,mode:MBProgressHUDMode.text)
    }
}
